<?php
namespace App\Common\Utill;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;

class OperationLog
{
    /**
     * 操作日志入库
     * @param $type //操作类型  1=添加 2=修改 3=删除 4=登录 5=退出 6=释放
     * @param $module //模块信息
     * @param $data_id //数据id
     * @param $new_data //新的数据
     * @param $old_data //旧的数据
     * @param $notes //注意
     * @return void
     */
    public static function record($type,$module,$data_id=0,$new_data='',$old_data='',$notes='')
    {
        $postData['operator_id'] = Auth::user()['id'];
        $postData['operator_name'] = Auth::user()['username'];
        $postData['operation_type'] = $type;
        $postData['module'] = $module;
        $postData['ip_address'] = request()->ip();
        $postData['currentRouteAction'] = Route::currentRouteAction();
        $postData['currentRouteName'] = Route::currentRouteName();
        $postData['data_id'] = $data_id;
        $postData['old_data'] = '';
        $postData['new_data'] = '';
        $postData['notes'] = $notes;
        if($old_data){
            $postData['old_data'] = json_encode($old_data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
        }
        if($new_data){
            $postData['new_data'] = json_encode($new_data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
        }
        $postData['created_at'] = date('Y-m-d H:i:s');
        DB::table("pd_operation_log")->insert($postData);
    }
}
